package com.lagou.dao.impl;

import com.lagou.dao.ResumeDao;
import com.lagou.pojo.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("resumeDao")
public class ResumeDaoImpl implements ResumeDao {
	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public void insert(Resume resume) {
		mongoTemplate.insert(resume, "lg_resume");
	}

	@Override
	public List<Resume> findAll() {
		return mongoTemplate.findAll(Resume.class, "lg_resume");
	}

	@Override
	public Resume findByName(String name) {
		Query query = new Query();
		query.addCriteria(Criteria.where("name").is(name));
		Resume resume = mongoTemplate.findOne(query, Resume.class, "lg_resume");
		return resume;
	}

	@Override
	public List<Resume> findByNameOrExpectSalary(String name, double expectSalary) {
		Query query = new Query();
		query.addCriteria(new Criteria().orOperator(Criteria.where("name").is(name), Criteria.where("expectSalary").is(expectSalary)));
		List<Resume> resumeList = mongoTemplate.find(query, Resume.class, "lg_resume");
		return resumeList;
	}

	@Override
	public void updateResume(String name, double expectSalary) {
		Query query = new Query();
		query.addCriteria(Criteria.where("name").is(name));
		Update update = new Update();
		update.set("expectSalary", expectSalary);
		mongoTemplate.updateMulti(query, update, "lg_resume");
	}

	@Override
	public void deleteResume(String name) {
		Query query = new Query();
		query.addCriteria(Criteria.where("name").is(name));
		mongoTemplate.remove(query, "lg_resume");
	}
}
